﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace uam_fps_game.Gfx
{
    public class Billboard : VisualObject
    {
        
        public bool IsTransparent { get; private set; }

        public Vector2 Scale { get; set; }

        static VertexPositionTexture[] _vertices= new VertexPositionTexture[4];
        static short[] _indices = new short[6];

        static Effect _effect;

        Texture2D _texture;

        static GraphicsDevice graphicsDevice;

        static Matrix _viewMatrix;
        static Matrix _projectionMatrix;

        public static void SetGraphicsDevice(GraphicsDevice device)
        {
            graphicsDevice = device;
        }

        public static void LoadEffect(ContentManager content)
        {
            // ladowanie efektu i ustawienie aktualnej techniki
            _effect = content.Load<Effect>("Shaders\\BillboardEffect").Clone();
            _effect.CurrentTechnique = _effect.Techniques["Technique1"];
        }

        public static void SetMatrices(Matrix view, Matrix projection)
        {
            _viewMatrix=view;
            _projectionMatrix=projection;
        }

        static Billboard()
        {

            _indices[0] = 0;
            _indices[1] = 1;
            _indices[2] = 3;
            _indices[3] = 0;
            _indices[4] = 3;
            _indices[5] = 2;

            
        }

        internal Billboard(Texture2D texture)
        {
            //Vector3 _localZ = position;
            _texture = texture;
        }

        public void Render()
        {
            _vertices[0] = new VertexPositionTexture(WorldMatrix.Translation, new Vector2(0, 1));
            _vertices[1] = new VertexPositionTexture(WorldMatrix.Translation, new Vector2(1, 1));
            _vertices[2] = new VertexPositionTexture(WorldMatrix.Translation, new Vector2(0, 0));
            _vertices[3] = new VertexPositionTexture(WorldMatrix.Translation, new Vector2(1, 0));

            RasterizerState rs = new RasterizerState();
            rs.CullMode = CullMode.None;
            graphicsDevice.RasterizerState = rs;
            // Ustawianie wartosci globalnych zmiennych shadera.
            //_effect.Parameters["World"].SetValue(WorldMatrix);
            _effect.Parameters["View"].SetValue(_viewMatrix);
            _effect.Parameters["Projection"].SetValue(_projectionMatrix);
            _effect.Parameters["diffuseTexture"].SetValue(_texture);
            _effect.Parameters["Scale"].SetValue(Scale);

            //_viewMatrix.Forward
            //_effect.Parameters["CameraPos"].SetValue(Matrix.Invert(_viewMatrix).Translation);
            _effect.Parameters["CameraPos"].SetValue(Matrix.Invert(_viewMatrix).Forward);


            graphicsDevice.DepthStencilState = DepthStencilState.Default;
            graphicsDevice.BlendState = BlendState.Opaque;

            // Dla kazdego "passa" w aktualnej technice - u nas jest tylko jeden pass,
            // wiec moznaby wykonac kod petli jedynie dla "myEffect.CurrentTechnique.Passes[0]"
            foreach (EffectPass pass in _effect.CurrentTechnique.Passes)
            {
                pass.Apply();

                // Najwazniejsza czesc rysowania - tzw. Draw Call - polecenia rysowania dla karty
                graphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, _vertices, 0, _vertices.Length, _indices, 0, _indices.Length / 3, VertexPositionTexture.VertexDeclaration);
            }
        }
    }
}
